package com.ruoyi.iot.modbus.tcp;

/**
 * @Description: Modbus4jReadUtils
 * @Author: Howe
 * @Date: 2019/11/28 11:20
 */

import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;

public class Modbus4jReadUtils {

    /**
     * @Description: 读取[01 Coil Status 0x]类型 开关数据
     * @Param: [master, slaveId, offset]master ,slaveId 位置
     * @Return: java.lang.Boolean 读取值
     * @Author: Howe
     * @Date: 2019/11/28 11:16
     */
    public static Boolean readCoilStatus(ModbusMaster master, int slaveId, int offset)
            throws ModbusTransportException, ErrorResponseException {
        BaseLocator<Boolean> loc = BaseLocator.coilStatus(slaveId, offset);
        Boolean value = master.getValue(loc);
        return value;
    }

    /**
     * @Description: 读取[02 Input Status 1x]类型 开关数据
     * @Param: [master, slaveId, offset]
     * @Return: java.lang.Boolean
     * @Author: Howe
     * @Date: 2019/11/28 11:18
     */
    public static Boolean readInputStatus(ModbusMaster master, int slaveId, int offset)
            throws ModbusTransportException, ErrorResponseException {
        BaseLocator<Boolean> loc = BaseLocator.inputStatus(slaveId, offset);
        Boolean value = master.getValue(loc);
        return value;
    }

    /**
     * @Description: 读取[03 Holding Register类型 2x]模拟量数据
     * @Param: [master, slaveId, offset, dataType]
     * @Return: java.lang.Number
     * @Author: Howe
     * @Date: 2019/11/28 11:18
     */
    public static Number readHoldingRegister(ModbusMaster master, int slaveId, int offset, int dataType)
            throws ModbusTransportException, ErrorResponseException {
        BaseLocator<Number> loc = BaseLocator.holdingRegister(slaveId, offset, dataType);
        Number value = master.getValue(loc);
        return value;
    }

    /**
     * @Description: 读取[04 Input Registers 3x]类型 模拟量数据
     * @Param: [master, slaveId, offset, dataType]
     * @Return: java.lang.Number
     * @Author: Howe
     * @Date: 2019/11/28 11:19
     */
    public static Number readInputRegisters(ModbusMaster master, int slaveId, int offset, int dataType)
            throws ModbusTransportException, ErrorResponseException {
        // 04 Input Registers类型数据读取
        BaseLocator<Number> loc = BaseLocator.inputRegister(slaveId, offset, dataType);
        Number value = master.getValue(loc);
        return value;
    }

    /**
     * @Description: 批量读取使用方法
     * @Param: [master]
     * @Return: void
     * @Author: Howe
     * @Date: 2019/11/28 11:20
     */
    public static void batchRead(ModbusMaster master) throws ModbusTransportException, ErrorResponseException {
        BatchRead<Integer> batch = new BatchRead<Integer>();
        batch.addLocator(0, BaseLocator.holdingRegister(2, 1, DataType.FOUR_BYTE_FLOAT));
        batch.addLocator(1, BaseLocator.inputStatus(1, 0));
        batch.setContiguousRequests(false);
        BatchResults<Integer> results = master.send(batch);
        System.out.println(results.getValue(0));
        System.out.println(results.getValue(1));
    }

    /**
     * @Description: 测试
     * @Param: [args]
     * @Return: void
     * @Author: Howe
     * @Date: 2019/11/28 11:20
     */
    // public static void main(String[] args) {
    //
    // try {
    // ModbusMaster master = CreateTCPMasterUtils.getMaster("RTU");
    // // 01测试
    //// Boolean v011 = readCoilStatus(1, 1);
    //// Boolean v012 = readCoilStatus(1, 1);
    //// Boolean v013 = readCoilStatus(1, 6);
    //// System.out.println("v011:" + v011);
    //// System.out.println("v012:" + v012);
    //// System.out.println("v013:" + v013);
    // // 02测试
    //// Boolean v021 = readInputStatus(1, 0);
    //// Boolean v022 = readInputStatus(1, 1);
    //// Boolean v023 = readInputStatus(1, 2);
    //// System.out.println("v021:" + v021);
    //// System.out.println("v022:" + v022);
    //// System.out.println("v023:" + v023);
    //
    // // 03测试
    // Number v031 = readHoldingRegister(master, 3, 0001, DataType.TWO_BYTE_BCD);
    // Number v032 = readHoldingRegister(1, 3, DataType.FOUR_BYTE_FLOAT);
    //
    // System.out.println("v031:" + v031);
    //// System.out.println("v032:" + v032);
    ////
    // // 04测试
    //// Number v041 = readInputRegisters(1, 0, DataType.FOUR_BYTE_FLOAT);//
    //// Number v042 = readInputRegisters(1, 2, DataType.FOUR_BYTE_FLOAT);//
    //// System.out.println("v041:" + v041);
    //// System.out.println("v042:" + v042);
    //// // 批量读取
    //// batchRead();
    //
    // } catch (Exception e) {
    // e.printStackTrace();
    // }
    // }

}